﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.Models;
using VehicleScale.EntityMappers;
using System.Diagnostics;

namespace VehicleScale.DataAccess
{
    public class ConfigDataAccess : BaseDataAccess, IConfigDataAccess
    {

        #region Properties

        /// <summary>
        /// Search Object Property
        /// </summary>
        public ConfigModel SearchObject
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
#endregion

        #region Methods
        /// <summary>
        /// Search Method
        /// </summary>
        /// <returns></returns>
        public IList<ConfigModel> Searcher()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get Method
        /// </summary>
        /// <returns></returns>
        public ConfigModel Get()
        {
            ConfigModel item = null;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    Config query = dataContext.Configs.FirstOrDefault();

                    if (query != null)
                    {
                        item = ConfigMapper.ToModel(query);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return item;
        }

        /// <summary>
        /// Get Last Method
        /// </summary>
        /// <returns></returns>
        public ConfigModel GetLast()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get All Method
        /// </summary>
        /// <returns></returns>
        public IList<ConfigModel> GetAll()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Add Method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(ConfigModel item)
        {
            bool result = false;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    Config entity = new Config();
                    ConfigMapper.ToEntity(entity, item);
                    dataContext.Configs.InsertOnSubmit(entity);
                    dataContext.SubmitChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// Add Collection Method
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public bool Add(IList<ConfigModel> collection)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Edit Item Method
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Edit(ConfigModel item)
        {
            bool result = false;
            try
            {
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    Config entity = dataContext.Configs.FirstOrDefault();
                    if (entity != null)
                    {
                        ConfigMapper.ToEntity(entity, item);
                        dataContext.SubmitChanges();
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return result;
        }

        public bool Edit(IList<ConfigModel> collection)
        {
            throw new NotImplementedException();
        }

        public bool Delete(ConfigModel item)
        {
            throw new NotImplementedException();
        }

        public bool Delete(IList<ConfigModel> collection)
        {
            throw new NotImplementedException();
        }

        #region Maintance
        public bool Backup(string databasename, string filename)
        {
            try
            {
                int i = 0;
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    i = dataContext.spBackupDatabase(databasename, filename);
                }
                return i == 0;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }

        public bool Restore(string databasename, string filename)
        {
            bool result = false;
            try
            {
                int i = 0;
                using (DataClassesDataContext dataContext = new DataClassesDataContext(base.ConnectionString))
                {
                    i = dataContext.spBackupDatabase(databasename, filename);
                }
                result = (i == 0);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return result;
        }
        #endregion

        #endregion

    }
}
